package com.gxa.controller;

import com.gxa.service.AddOrderDataService;
import com.gxa.service.XsDdService;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/xsDd")
@Api(value = "执行销售操作的控制器",tags = "销售接口的Api")
@CrossOrigin  //跨域处理
public class XsDdController {

    @Resource
    XsDdService xsDdService;

    @Resource
    AddOrderDataService addOrderDataService;

    // number 页码    size：每页显示记录数

    /**
     * 订单列表分页数据
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllXsDdByPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询销售列表分页接口", notes = "获取所有的销售数据列表", httpMethod = "post", response = String.class)
    public Map findAllXsDdByPage(@RequestBody(required = false) Map map) {
        JsonResult jsonResult = new JsonResult();
        try {
            String number = map.get("number").toString();
            String size = map.get("size").toString();
            Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
            Integer limit = Integer.parseInt(size);
            map.put("offset", offset);
            map.put("limit", limit);
            List<Map<String, Object>> list = xsDdService.selectAllByPage(map);
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 销售订单列表总条数查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllXsDd", method = RequestMethod.POST)
    public Map findAllXsDd(Map map) {
        JsonResult jsonResult = new JsonResult();
        Integer count = xsDdService.findAllXsDd(map);
        if (count > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(count);
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }


    /**
     * 根据id预览订单详情
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Map selectById(@RequestBody(required = false) Map map) {
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> xsDd = xsDdService.selectById(Integer.valueOf(map.get("id").toString()));
        List<Map<String, Object>> aod = addOrderDataService.selectXid(Integer.valueOf(map.get("id").toString()));
        jsonResult.put("xsDd", xsDd);
        jsonResult.put("addOrderData", aod);
        try {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }


    /**
     * 查询所有客户名称
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByClientsName", method = RequestMethod.POST)
    public Map<String, Object> selectByClientsName() {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> clientsName = xsDdService.selectByClientsName();
        if (clientsName != null && !clientsName.equals("")) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(clientsName);
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 查询所有客户类型
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByTypeOfCustomer", method = RequestMethod.POST)
    public Map<String, Object> selectByTypeOfCustomer() {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> typeOfCustomer = xsDdService.selectByTypeOfCustomer();
        if (typeOfCustomer != null && !typeOfCustomer.equals("")) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(typeOfCustomer);
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }


    /**
     * 查询所有业务员
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectBySalesMan", method = RequestMethod.POST)
    public Map<String, Object> selectBySalesMan() {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> salesMan = xsDdService.selectBySalesMan();
        if (salesMan != null && !salesMan.equals("")) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(salesMan);
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 查询所有销售类型
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByTypeOfSales", method = RequestMethod.POST)
    public Map<String, Object> selectByTypeOfSales() {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> typeOfSales = xsDdService.selectByTypeOfSales();
        if (typeOfSales != null && !typeOfSales.equals("")) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(typeOfSales);
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 查询所有出库仓库
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByWareHouse", method = RequestMethod.POST)
    public Map<String, Object> selectByWareHouse() {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> wareHouse = xsDdService.selectByWareHouse();
        if (wareHouse != null && !wareHouse.equals("")) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(wareHouse);
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 根据id删除订单
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Map deleteById(@RequestBody(required = false) Map map) {
        JsonResult jsonResult = new JsonResult();
        int xsDd = xsDdService.deleteById(map);
        int aod = addOrderDataService.deleteById(Integer.valueOf(map.get("id").toString()));
        if (xsDd > 0 && aod > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 批量删除
     *
     * @param s
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteByParams", method = RequestMethod.POST)
    public Map deleteByParams(@RequestBody(required = false) Map<String, List> s) {
        JsonResult jsonResult = new JsonResult();
        int aod = 0;
        List ids = s.get("id");
        int xsDd = xsDdService.deleteByParams(ids);
        for (Object id : ids) {
            aod = addOrderDataService.deleteById((Integer) id);
        }
        if (xsDd > 0 && aod > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        } else {
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }


    /**
     * 销售列表多条件查询
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByLike", method = RequestMethod.POST)
    public Map<String, Object> selectByLike(@RequestBody(required = false) Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();
        if (map.get("startDate") != null && !map.get("startDate").equals("") && map.get("endDate") != null && !map.get("endDate").equals("")
        ) {
            try {
                String number = map.get("number").toString();
                String size = map.get("size").toString();
                Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
                Integer limit = Integer.parseInt(size);
                map.put("offset", offset);
                map.put("limit", limit);
                List<Map<String, Object>> mapList = xsDdService.selectByLike(map);
                Integer likeCount = xsDdService.selectByLikeCount(map);
                jsonResult.put("likeCount", likeCount);
                jsonResult.setCode("200");
                jsonResult.setMsg("检索成功");
                jsonResult.setData(mapList);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setCode("405");
                jsonResult.setMsg("系统开小差了");
            }
        }else {
            try {
                String number = map.get("number").toString();
                String size = map.get("size").toString();
                Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
                Integer limit = Integer.parseInt(size);
                map.put("offset", offset);
                map.put("limit", limit);
                List<Map<String, Object>> mapList = xsDdService.selectAllByPage(map);
                Integer likeCount = xsDdService.findAllXsDd(map);
                jsonResult.put("likeCount", likeCount);
                jsonResult.setCode("200");
                jsonResult.setMsg("检索成功");
                jsonResult.setData(mapList);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setCode("406");
                jsonResult.setMsg("系统开小差了");
            }
        }
        return jsonResult.getValues();
    }
}